package cn.wch.ch934xlib.utils;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;

import cn.wch.ch934xlib.chip.ChipValidUtil;
import cn.wch.ch934xlib.constant.Constant;


public class UsbUtil {

    public static ArrayList<UsbDevice> getCurrentDevices(@NonNull UsbManager usbManager){
        ArrayList<UsbDevice> devices=new ArrayList<>();
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        for (UsbDevice device : deviceList.values()) {
            if(ChipValidUtil.isValid(device)){
                devices.add(device);
            }
        }
        return devices;
    }

    public static boolean hasPermission(@NonNull UsbManager usbManager,@NonNull UsbDevice device){
        return usbManager.hasPermission(device);
    }

    public static void requestPermission(@NonNull UsbManager usbManager,@NonNull Context context,@NonNull UsbDevice device,int requestCode){
        Intent intent=new Intent(Constant.REQUEST_PERMISSION_STRING);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(context, requestCode, intent, 0);
        usbManager.requestPermission(device,pendingIntent);
    }
}
